<?php

class QgeneralController extends Controller {

    public $layout = "application.views.layouts.admin";

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('Page'),
                'roles' => array('admin')
            ),
            array('deny',
                'actions' => array('Page'),
                'roles' => array('guest', 'user')
            )
        );
    }


	
	
	
        //function find and add two state to index admin page
        public function actionState()
        {
            $state=State::model()->findAll('enabled=:enabled',array(':enabled'=>1));
            $this->render('application.views.qgeneral.state',array('state'=>$state));
        }

        //function check id editing state, and render editor viewer
        public function actionEdit()
        {
            $id=$_GET['id'];
            $state=State::model()->findByPk($id);
            $this->render('application.views.qgeneral.state',array("state"=>$state));
        }

        //function update state to data base
        public function actionUpdate()
        {
            if ( (isset($_POST['title'])) && (isset($_POST['redactor_content'])) )
            {
                $id=$_POST['id'];
                $state=State::model()->findByPk($id);

                $state->title=$_POST['title'];
                $state->text=$_POST['redactor_content'];

                if ($state->save())
                {
                    $this->redirect('/admin/qgeneral');
                }
                else
                {
                    echo 'some thing wrong';
                }
            }
        }

        public function actionIndex()
        {
        	$question_unread = question::model()->count('readed=:readed',array(':readed'=>0));
			$images = images::model()->findAll();
            $state=State::model()->findAll('enabled=:enabled',array(':enabled'=>1));
            $this->render('application.views.qgeneral.page',array('state'=>$state,'question_unread'=>$question_unread,'images'=>$images));
        }
}
?>